package com.example.myactspringboot.server;

import com.example.myactspringboot.bean.SysMenu;
import com.example.myactspringboot.bean.SysRoleMenu;
import com.example.myactspringboot.bean.SysUser;
import com.example.myactspringboot.dao.SysMenuMapper;
import com.example.myactspringboot.dao.SysUserMapper;
import com.example.myactspringboot.util.TimeFormatKeyGenerator;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Create 2023-09-23 15:46
 * @Description
 */
@Service
public class SysMenuService {
    @Autowired
    SysMenuMapper menuMapper;
    @Autowired
    SysUserMapper userMapper;

    public List<SysMenu> getMenus(String username) {
        List<SysMenu> menuList = new ArrayList<>();
        SysUser userByName;
        if (username.equals("")) {
            menuList = menuMapper.getMenuList();
        } else {
            userByName = userMapper.getUserByName(username);
            menuList = menuMapper.getMenuListByUserId(userByName.getId());
        }
        Map<String, SysMenu> maps = new HashMap<>();
        menuList.stream().map(item -> maps.put(item.getId(), item)).collect(Collectors.toList());
        ArrayList<SysMenu> resultList = new ArrayList<>();
        for (SysMenu sysMenu : menuList) {
            if (!sysMenu.getParentId().equals("0")) {
                maps.get(sysMenu.getParentId()).getChildren().add(sysMenu);
            } else {
                resultList.add(sysMenu);
            }
        }
        Collections.sort(resultList, new Comparator<SysMenu>() {
            @Override
            public int compare(SysMenu o1, SysMenu o2) {
                if (o1.getSort() == null && o2.getSort() == null) {
                    return 0;
                } else if (o1.getSort() == null && o2.getSort() != null) {
                    return -1;
                } else if (o1.getSort() != null && o2.getSort() == null) {
                    return 1;
                } else {
                    return o1.getSort() > o2.getSort() ? 1 : o1.getSort() == o2.getSort() ? 0 : -1;
                }
            }
        });
        return resultList;
    }

    /*新增*/
    public int saveMenu(SysMenu menu) {
        menu.setId(TimeFormatKeyGenerator.getInstance().getNext());
        Session session = SecurityUtils.getSubject().getSession();
        menu.setCreateUser(session.getAttribute("username").toString());
        menu.setCreateTime(new Date());
        return menuMapper.saveMenu(menu);
    }

    /*更新菜单*/
    public int updateMenu(SysMenu menu) {
        Session session = SecurityUtils.getSubject().getSession();
        menu.setUpdateUser(session.getAttribute("username").toString());
        menu.setUpdateTime(new Date());
        return menuMapper.updateMenu(menu);
    }

    /*非树*/
    public List<SysRoleMenu> getMenusNoTree(String roleId) {
        List<SysRoleMenu> menuList = menuMapper.getRoleMenuByRoleId(roleId);
        return menuList;
    }

    @Transactional
    public Integer updateRoleMenu(List<String> list,String roleId){
        int i = menuMapper.delRoleMenuByRoleId(roleId);
        int i1 = menuMapper.saveRoleMenu(list, roleId);
        return i1;
    }

}
